package com.qinge.blog.controller;

import com.qinge.blog.constant.AnnotationConstant;
import com.qinge.blog.constant.Constant;
import com.qinge.blog.service.BlogUploadService;
import com.qinge.blog.utils.result.JsonResult;
import com.qinge.blog.utils.result.ResultTool;
import com.qinge.blog.vo.file.UploadReqVO;
import com.qinge.blog.vo.file.UploadRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author QDW
 * @date 2022/5/15 15:02
 * @description
 **/
@Api(tags = {"文件上传"})
@RestController
public class BlogUploadController {

    @Autowired
    private BlogUploadService uploadService;

    /**
     * 文件上传
     * @param reqDTO
     * @return
     */
    @PostMapping("/common/api/file/upload")
    @ApiOperation(value = "文件上传", notes = "此接口较为特殊，参数都通过表单方式提交，而非JSON")
    public JsonResult<UploadRespVO> upload(@ModelAttribute UploadReqVO reqDTO) {
        // 上传并返回URL
        UploadRespVO respDTO = uploadService.upload(reqDTO);
        return ResultTool.success(respDTO);
    }

    /**
     * 独立文件下载
     * @param request
     * @param response
     */
    @GetMapping(value = AnnotationConstant.FILE_PREFIX+ "**")
    @ApiOperation(value = "文件下载", notes = "文件下载")
    public void download(HttpServletRequest request, HttpServletResponse response) {
        uploadService.download(request, response);

    }
}
